package com.l2qq.admin.account;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.l2qq.admin.permission.PermissionAuth;
import com.l2qq.admin.permission.PermissionGroup;
import com.l2qq.common.BaseController;

/**
 * 账户管理控制层
 */
@Path("/admin/account")
@PermissionGroup(value = "用户管理", description = "新增账户、删除账户、冻结账户")
public class AccountController extends BaseController {

    @Inject
    AccountService accountService;

    @PermissionAuth(value = "新增修改", remark = "查看所有用户信息", key = "account:saveOrUpdate")
    public void saveOrUpdate() {
        JSONObject jsonObject = JSONObject.parseObject(getRawData(), Feature.OrderedField);
        Ret ret = accountService.saveOrUpdate(jsonObject);
        renderJson(ret);
    }

    @PermissionAuth(value = "查询列表", remark = "查询账户列表", key = "account:searchByPage")
    public void searchByPage() {
        JSONObject jsonObject = JSONObject.parseObject(getRawData(), Feature.OrderedField);
        renderJson(accountService.searchByPage(jsonObject));
    }

    @PermissionAuth(value = "查询头部", remark = "查询账户列表-搜索头部", key = "account:searchByHeard")
    public void searchByHeard() {
        JSONObject jsonObject = JSONObject.parseObject(getRawData(), Feature.OrderedField);
        Ret ret = accountService.searchByHeard(jsonObject);
        renderJson(ret);
    }


    @PermissionAuth(value = "详细", remark = "查询账户-详细", key = "account:detail")
    public void detail() {
        Ret ret = accountService.detail(getInt("id"));
        renderJson(ret);
    }

/*    @PermissionAuth(value = "修改密码", remark = "账户-修改密码", key = "user:delete", dependencies = {@PermissionDependency({"account:detail"}) // 依赖多个权限
    })
    public void changePassWord() {
        String jsonStr = getRawData();
        Account account = JSON.parseObject(jsonStr, Account.class);
        if (StrKit.notBlank(account.getPassword())) {
            accountService.passwordSaltAndHash(account);
            //account.setIsInitialPw(false);
            loginAdminService.roleVersion++;
            String sql = "update account set realname = ?,user_name = ?, pass_word = ?,salt = ?, updated = ?,shortcut_key = ?,is_initial_pw = false,billing_company_id = ? where id = ?";
            Db.update(sql, account.getRealname(), account.getUsername(), account.getPassword(), account.getSalt(), new Date(), account.getShortcutKey(), account.getBillingCompanyId(), getLoginAccountId());
        } else {
            Account acc = accountService.getAccountById(getLoginAccountId());
            account.setPassword(acc.getPassword());
            account.setSalt(acc.getSalt());
            String sql = "update account set realname = ?,user_name = ?, pass_word = ?,salt = ?, updated = ?,shortcut_key = ?,billing_company_id = ? where id = ?";
            Db.update(sql, account.getRealname(), account.getUsername(), account.getPassword(), account.getSalt(), new Date(), account.getShortcutKey(), account.getBillingCompanyId(), getLoginAccountId());
        }
        renderJson(Ret.ok().set("msg", "修改成功！"));
    }*/

    @PermissionAuth(value = "删除用户", remark = "账户-删除用户", key = "account:delete")
    public void deleteById() {
        Ret ret = accountService.deleteById(getLoginAccountId(), getInt("id"));
        renderJson(ret);
    }

    @PermissionAuth(value = "修改密码", remark = "账户-修改密码", key = "account:changePassword")
    public void changePassword(){
        JSONObject jsonObject = JSONObject.parseObject(getRawData(), Feature.OrderedField);
        Ret ret = accountService.changePassword(jsonObject);
        renderJson(ret);
    }

}



